package com.softeem.dao.impl;

import com.softeem.bean.Oss;
import com.softeem.dao.OssDao;
import com.softeem.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OssDaoImpl extends BaseDao<Oss> implements OssDao {

    @Override
    public int addOss(Oss book)  throws SQLException{
        return this.update("INSERT INTO space_oss_info (bucket_name, type,permission) VALUES (?, ?, ?)",
                book.getbucketName(),book.gettype(),book.getpermission()
        );
    }

    @Override
    public int deleteOssId(Integer id) throws SQLException {
        return this.update("delete from space_oss_info where id = ?" , id);
    }

    @Override
    public int updateOss(Oss book) throws SQLException {
        return this.update("update space_oss_info set bucket_name=?,type=?,permission=? where id = ?",
                book.getbucketName(),book.gettype(),book.getpermission(),book.getId()
        );
    }

    @Override
    public int updatePermission(Integer id, String permission) throws SQLException {
        return this.update("update space_oss_info set permission=? where id = ?",
                permission,id);
    }

    @Override
    public Oss queryOssById(Integer id) throws SQLException {
        return this.getBean("select * from space_oss_info where id = ?" , id);
    }

    @Override
    public Oss queryOssByName(String name) throws SQLException {
        return this.getBean("select * from space_oss_info where bucket_name = ?" , name);
    }

    @Override
    public List<Oss> queryOss() throws SQLException {
        return this.getBeanList("select * from space_oss_info order by capacity asc");
    }

    /**
     * 此方法用来获取表中的总记录数
     * @return
     */
    @Override
    public Integer queryForPageTotalCount() throws SQLException {
        return this.getValue("select count(*) from space_oss_info").intValue();
    }

    /**
     * 分页查询方法
     * @param begin 起始下标
     * @param pageSize 显示数据条数
     * @return
     */
    @Override
    public List<Oss> queryForPageItems(int begin, int pageSize)  throws SQLException{
        return this.getBeanList("select * from space_oss_info order by capacity asc limit ?,?" , begin , pageSize);
    }

    @Override
    public Integer queryForPageTotalCount(String name, String author, Double min, Double max)  throws SQLException{
        StringBuilder sql = new StringBuilder("select count(*) from space_oss_info where 1=1 ");
        ArrayList params = new ArrayList();//保存参数的
        System.out.println(sql);
        //
        return this.getValue(sql.toString(),params.toArray()).intValue();
    }

    @Override
    public List<Oss> queryForPageItems(int begin, int pageSize, String name, String author, Double min, Double max)  throws SQLException{
        StringBuilder sql = new StringBuilder("select * from space_oss_info where 1=1 ");
        ArrayList params = new ArrayList();//保存参数的
        if(name != null && !name.isEmpty()){
            sql.append(" AND bucket_name like CONCAT('%', '"+name+"', '%')");
        }
        sql.append(" order by bucket_name asc limit ? , ?");
        params.add(begin);
        params.add(pageSize);
        System.out.println(sql);
        return this.getBeanList(sql.toString(),params.toArray());
    }

}
